package com.novasoftware.novagooglemap.framework.activity;

import java.util.ArrayList;

import com.novasoftware.novagooglemap.framework.util.SystemUiHider;

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;


public abstract class BaseActivity extends Activity {

	private SystemUiHider mSystemUiHider;
	
	protected boolean AUTO_HIDE = true;

	protected int AUTO_HIDE_DELAY_MILLIS = 3000;

	protected boolean TOGGLE_ON_CLICK = true;

	protected int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION;
	
	protected abstract int getContent();
	protected abstract void LocalizeControls();
	
	protected View GetControlsView(){return null;}
	protected View GetUIView(){ return null;}
	protected View GetDelayHideView(){ return null;}
	
	protected void RegisterAdapter(){}
	protected void PopulateData(){}
	protected void InitializeData(){}
	protected void RegisterEvents(){}
	protected void InitializeService() {}
	protected void InitializeControls(){}
	protected abstract boolean IsHiderUI();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    InitializeService();
	    setContentView(getContent());
	    InitializeData();
	    if(IsHiderUI()) SetUIHider();
	    LocalizeControls();
	    PopulateData();
	    RegisterAdapter();
	    InitializeControls();
	    RegisterEvents();
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		if(IsHiderUI()) delayedHide(100);
	}
	
	protected void SetControlsView() {
		if (TOGGLE_ON_CLICK) {
			mSystemUiHider.toggle();
		} else {
			mSystemUiHider.show();
		}
	}

	Handler mHideHandler = new Handler();
	Runnable mHideRunnable = new Runnable() {
		@Override
		public void run() {
			mSystemUiHider.hide();
		}
	};

	View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
		@Override
		public boolean onTouch(View view, MotionEvent motionEvent) {
			if (AUTO_HIDE) {
				delayedHide(AUTO_HIDE_DELAY_MILLIS);
			}
			return false;
		}
	};
	
	private void SetUIHider() {
		mSystemUiHider = SystemUiHider.getInstance(this, GetUIView(),
				HIDER_FLAGS);
		mSystemUiHider.setup();
		mSystemUiHider
				.setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() {
					// Cached values.
					int mControlsHeight;
					int mShortAnimTime;

					@Override
					@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
					public void onVisibilityChange(boolean visible) {
						if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
							if (mControlsHeight == 0) {
								mControlsHeight = GetControlsView().getHeight();
							}
							if (mShortAnimTime == 0) {
								mShortAnimTime = getResources().getInteger(
										android.R.integer.config_shortAnimTime);
							}
							GetControlsView()
									.animate()
									.translationY(visible ? 0 : mControlsHeight)
									.setDuration(mShortAnimTime);
						} else {
							GetControlsView().setVisibility(visible ? View.VISIBLE
									: View.GONE);
						}

						if (visible && AUTO_HIDE) {
							delayedHide(AUTO_HIDE_DELAY_MILLIS);
						}
					}
				});
		GetDelayHideView().setOnTouchListener(
				mDelayHideTouchListener);
	}

	private void delayedHide(int delayMillis) {
		mHideHandler.removeCallbacks(mHideRunnable);
		mHideHandler.postDelayed(mHideRunnable, delayMillis);
	}
}
